package com.wyq.util.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wyq.util.JDBCDAOSupport;

public class JDBCDAOSupportImpl  extends HibernateDaoSupport implements JDBCDAOSupport{

	public Session getHibernateSession() {
		return getSession(getHibernateTemplate().isAllowCreate());
	}
	
	public void realesHibernateSession(Session session) {
		this.releaseSession(session);
		
	}
	
	public List getDataBySql(String sql) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}


	public List getListMapBySql(final String sql) throws Exception {
		
		this.getHibernateTemplate().setAllowCreate(true);
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {				
			public Object doInHibernate(Session arg0) throws HibernateException,
					SQLException {
				try{
										
					SQLQuery sqlQuery = arg0.createSQLQuery(sql);
					sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
					List result = sqlQuery.list();
					return result;
				}catch (Exception e) {
					throw new RuntimeException(e.getMessage(),e);
				}
			}
		});
		
	}

	public int execupdateBySql(final String sql) throws Exception {
		this.getHibernateTemplate().setAllowCreate(true);
		return (Integer)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session arg0) throws HibernateException,
					SQLException {
				int result =-1;
				try{
					//arg0 = getHibernateTemplate().getSessionFactory().openSession();
					SQLQuery sqlQuery = arg0.createSQLQuery(sql);
					result = sqlQuery.executeUpdate();
					return new Integer(result);
				}catch (Exception e) {
					
					throw new RuntimeException(e.getMessage(),e);
					
				}finally{
					//releaseSession(arg0);
					
				}
			}});
	}
	

}
